﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Entities;

namespace Yglu.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader Product_Get()
		{
			DataHandler db = new DataHandler();

			return (SqlDataReader)db.ExecuteReader("upProduct_Get");
		}

		public static SqlDataReader Product_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@ProductId", id);

			return (SqlDataReader)db.ExecuteReader("upProduct_Get");
		}

		public static SqlDataReader Product_Get(Store store)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@StoreId", store.Id);

			return (SqlDataReader)db.ExecuteReader("upProduct_Get");
		}

		public static SqlDataReader Product_Get(Category category)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@CategoryId", category.Id);

			return (SqlDataReader)db.ExecuteReader("upProduct_Get");
		}

		public static SqlDataReader Product_Get(Combo combo)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@ComboId", combo.Id);

			return (SqlDataReader)db.ExecuteReader("upProduct_Get");
		}

		public static SqlDataReader Product_Get(ReadyCombo rcombo)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@ReadyComboId", rcombo.Id);

			return (SqlDataReader)db.ExecuteReader("upProduct_Get");
		}

		#endregion

		#region Insert and update

		public static void Product_Upsert(Product product)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@CategoryId", product.Category.Id);
				db.AddParameter("@Title", product.Title);
				db.AddParameter("@Price", product.Price);
				db.AddParameter("@AllStores", product.AllStores);
				db.AddParameter("@Active", product.Active);

				if (product.StoreId > 0)
				{
					db.AddParameter("@StoreId", product.StoreId);
				}

				if (product.Id > 0)
				{
					db.AddParameter("@ProductId", product.Id);
                    db.ExecuteScalar("upProduct_Update");
				}
				else
				{
					product.Id = Convert.ToInt32(db.ExecuteScalar("upProduct_Insert"));
				}
			}
		}

		#endregion
	}
}